<?php
/**
 * Created by PhpStorm.
 * User: lelouchms
 * Date: 2019/3/28
 * Time: 17:22
 */

namespace app\common\exception\handler;


use app\common\exception\AuthenticationException;
use app\common\exception\LoginException;
use app\common\exception\ValidateException;
use app\common\model\OrderNotifyLog;
use Exception;
use think\Db;
use think\exception\Handle;

class ApiExceptionHandler extends Handle
{
    public function render(Exception $e)
    {
        $code = 400;
        $message = $e->getMessage();
        if ($e instanceof ValidateException) $code = 401;
        if ($e instanceof AuthenticationException) $code = 403;
        if ($e instanceof LoginException) {
            $code = $e->getMessage();
            switch ($e->getMessage()) {
                case LoginException::LOGIN_NONE:
                    $message = "请先登录";
                    break;
                case LoginException::LOGIN_EXPIRE:
                    $message = "登录超时,请重新登录";
                    break;
                case LoginException::USERNAME_NOT_EXISTS:
                    $message = "用户不存在";
                    break;
                case LoginException::USER_DISABLED:
                    $message = "用户已被禁用";
                    break;
                case LoginException::PASSWORD_ERROR:
                    $message = "密码错误";
                    break;
                default:
                    $message = "登录异常";
                    break;
            }
        }
        //其它此交由系统处理
        return json(["code" => $code, "message" => $message]);
    }


}